<?php
/**
 * 
 * 
 * @author
 * @version 
 */
class Admin_SettingsController extends Zen_Controller_Admin {

	
	
	public function indexAction(){
		
		$file=ROOT_PATH."/site/settings/settings.ini";
		Zen_Style::add('/cms/res/admin/css/forms.css');
		
		$values=new Zend_Config_Ini($file,APPLICATION_ENV);
		$params=parse_ini_file($file);
		
		$defaults=array();
		foreach ($params as $key=>$value){
			$key=preg_replace('/\./','_',$key);
			$defaults[$key]=$value;
		}
		
		$form=new Admin_Form_Settings();
		$form->setDefaults($defaults);
		
		if($this->_request->isPost()){
			if($form->isValid($this->_getAllParams())){
				
				$values=$form->getValues();
				$data=array();
				
				if (sizeof ( $form->getSubForms () ) > 0) {
					$data = array ();
					foreach ( $form->getSubForms () as $subform ) {
						$data = array_merge ( $data, $values [$subform->getName ()] );
					}
					$values = $data;
				}
				$data=array();
				foreach($values as $key=>$value){
					$key=preg_replace('/_/','.',$key);
					$data[$key]=$value;
				}
				if(Zen_User::getId()){
					if($values['lock']){
						$data['loker']=Zen_User::getId();
					}
				}
				Cache_Model_Clear::clear('pages');					

				$config = new Zend_Config(array(), true);
				$config->production = $data;
				$config->development = array();
				$config->setExtend('development', 'production');
				$writer=new Zend_Config_Writer_Ini();
				$writer->write($file,$config);
				$this->_helper->FlashMessenger->setNamespace ( 'messages' )->addMessage ( array ('text' => 'Изменения сохранены', 'type' => 'alert' ) );
				$this->_redirect($_SERVER['REQUEST_URI']);
			}
		}
		
		$this->view->form=$form;
		
	}
	
	
	
	
	
	
	
}
